package com.gutils.optimisation {

    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.KeyboardEvent;
    import flash.system.System;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.utils.getTimer;

    public class FPS extends Sprite {
	
		public static const MEMORY_BYTES:uint 		= 1;
		public static const MEMORY_KILOBYTES:uint 	= 2;
		public static const MEMORY_MEGABYTES:uint	= 3;
		
		private var _barColor:uint 	= 0xFFFFFF;
		private var _bgColor:uint 	= 0xCCCCCC;
		private var _textColor:uint = 0x333333;
		private var _bgAlpha:uint 	= 1;      
        private var _bg:Sprite 		= new Sprite();
        private var _text:TextField = new TextField();
		
        private var _prevFrameTime:Number 	= getTimer();       
        private var _prevSecondTime:Number 	= getTimer();
        
        private var _fps:String = "...";
		private var _frames:Number = 0;
		
		private var _time:Number;
		private var _secondTime:Number;		
		private var _memory:String;
		private var _typeMemory:uint;
		
		/**
		 * 
		 * @param typeMemory type of the memory displayed (bytes, kilobytes, megabytes)
		 * 
		 */
        public function FPS(typeMemory:uint = 2) 
		{			
			mouseChildren = false;
			
            _typeMemory = typeMemory;
          
			addEventListener(Event.ADDED_TO_STAGE, init);			
        }
	
		private function init(e:Event):void 
		{		
			removeEventListener(Event.ADDED_TO_STAGE, init, false);
            _bg.graphics.beginFill(_bgColor, 1);
            _bg.graphics.drawRect(0, 0, 100, 10);
            _bg.graphics.endFill();
            _bg.alpha = _bgAlpha;
            addChild(_bg);
           
            _text.autoSize=TextFieldAutoSize.LEFT;
            _text.textColor = _textColor;
            _text.selectable = false;
            addChild(_text);
            scaleX = 2;
            scaleY = 2;
            addEventListener(Event.ENTER_FRAME, onEnterFrame, false, 0, true);
		}
		
        private function onEnterFrame(e:Event):void {
            _time = getTimer();           
            _secondTime = _time - _prevSecondTime;
            if(_secondTime >= 1000){
                _fps = _frames.toString();
                _frames = 0;
                _prevSecondTime = _time;
			}
			else _frames++;
            _prevFrameTime = _time;
            if(_typeMemory == FPS.MEMORY_BYTES){
                _memory = flash.system.System.totalMemory.toPrecision(4);
                _memory = convert(Number(_memory)) + " bytes";
            }
			else if(_typeMemory == FPS.MEMORY_KILOBYTES){
                _memory = (flash.system.System.totalMemory / 1000).toPrecision(4);
                _memory = convert(Number(_memory)) + " kbs";
            }
			else if (_typeMemory ==  FPS.MEMORY_MEGABYTES){
                _memory = (flash.system.System.totalMemory / 1000000).toPrecision(4);
                _memory = convert(Number(_memory)) + " mbs";
            }
            _text.htmlText = "<font face=\"arial\" size=\"5\"> Framerate: "+ _fps +" fps <br/><br/> Memory: "+ _memory +"</font>";           
        }
        
        private function convert(value:Number):Number {
        	return Math.round(value * Math.pow(10, 2)) / Math.pow(10, 2);
        }
       
	}
}
